extends Component
var _player:CharacterBody2D
@onready var state_chart: StateChart = $StateChart
func _ready() -> void:
	_player=host
	cnt()
func _enable():
	set_physics_process(true)
	if not Global.player_movment_mode.is_connected(state_chart.send_event):Global.player_movment_mode.connect(state_chart.send_event)
func _disable():
	set_physics_process(false)
	if Global.player_movment_mode.is_connected(state_chart.send_event):Global.player_movment_mode.disconnect(state_chart.send_event)
#==========================================================================
func _on_static_state_physics_processing(delta: float) -> void:
	pass # Replace with function body.

#============================================================================
var walk_speed:float = 400
var swim_speed:float=800
func cnt()->void:
	Global.player_walk_speed.connect(func(speed:float):
		walk_speed=speed
		)
func _on_walk_state_physics_processing(delta: float) -> void:
	if _player.get_gravity()!=Vector2.ZERO:
		if _player.get_gravity()==Vector2(9.8,0.0):
			_player.up_direction=Vector2(-1,0)
			if not _player.is_on_floor():
				_player.velocity+=_player.get_gravity()
			if Input.is_action_just_pressed("jump") and _player.is_on_floor():
				_player.velocity.x=-400
			var dir:float=-Input.get_axis("a","d")
			if dir:
				_player.velocity.y = move_toward(_player.velocity.y,dir*walk_speed,delta*2000)
			else:
				_player.velocity.y=move_toward(_player.velocity.y,0,delta*1000)
		else:
			_player.up_direction=Vector2(0,-1)
			if not _player.is_on_floor():
				_player.velocity+=_player.get_gravity()
			if Input.is_action_just_pressed("jump") and _player.is_on_floor():
				_player.velocity.y=-400
			var dir:float=Input.get_axis("a","d")
			if dir:
				_player.velocity.x = move_toward(_player.velocity.x,dir*walk_speed,delta*2000)
			else:
				_player.velocity.x=move_toward(_player.velocity.x,0,delta*1000)
	else:
		var dir=Vector2(Input.get_axis("a","d"),Input.get_axis("w","s"))
		if dir:
			_player.velocity = _player.velocity.move_toward(dir*swim_speed,delta*3000)
		else:
			_player.velocity=_player.velocity.move_toward(Vector2.ZERO,delta*3000)

	_player.move_and_slide()

#===============================================================================
#tie
var tie_pos:Vector2
var tie_len:float = 50
var tie_gravity:float=0.98
func _on_tie_state_entered() -> void:
	tie_pos=_player.position
	if not _player.is_on_floor():
		_player.velocity.y=0
		tie_gravity=0.98
func _on_tie_state_physics_processing(delta: float) -> void:
	if not _player.is_on_floor():
		_player.velocity.y+=tie_gravity
	elif tie_gravity!=98:tie_gravity=98
	var dir = Input.get_axis("a","d")
	var dis=abs(_player.position.x-tie_pos.x)
	if dir and dis<tie_len:
		_player.velocity.x=dir*walk_speed
		if (tie_pos.x-_player.position.x)*dir<0:_player.velocity.x*=(tie_len-dis)/tie_len
	else:
		if dis>tie_len/10:
			var direc=(tie_pos.x-_player.position.x)/tie_len
			_player.velocity.x=walk_speed*direc
		else:_player.velocity.x=0
	if Input.is_action_just_pressed("jump") and _player.is_on_floor():
		_player.velocity.y=-400
	_player.move_and_slide()

#==============================================================================
#suspend
var _can_rush:bool=true
func _rush(dir:Vector2,strength:float)->void:
	if not _can_rush:return
	_player.velocity=dir.normalized()*strength
	_can_rush=false
	$"../../Display/RushCircle".visible=false
	await get_tree().create_timer(1).timeout
	_can_rush=true
	if _player.now_move_mod=="suspend":$"../../Display/RushCircle".visible=true

func _on_suspend_state_entered() -> void:
	_player.now_move_mod="suspend"
	Global.player_suspend_rush.connect(_rush)
	Global.player_suspend_change.emit(true)
	%RushTail.enable=true
	$"../../Display/RushCircle".visible=true
func _on_suspend_state_physics_processing(delta: float) -> void:
	if _player.velocity.length()>90:
		_player.velocity=_player.velocity.move_toward(Vector2.ZERO,delta*5000)
	else:
		_player.velocity=_player.velocity.move_toward(Vector2.ZERO,delta)
	_player.move_and_slide()

func _on_suspend_state_exited() -> void:
	_player.now_move_mod=""
	Global.player_suspend_rush.disconnect(_rush)
	Global.player_suspend_change.emit(false)
	%RushTail.enable=false
	$"../../Display/RushCircle".visible=false

#===============================================================================
#run


func _on_run_state_entered() -> void:
	pass # Replace with function body.


func _on_run_state_physics_processing(delta: float) -> void:
	
	_player.move_and_slide()
	pass # Replace with function body.


func _on_run_state_exited() -> void:
	pass # Replace with function body.
